﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace ArgusControls
{
	[TemplatePart(Name = PartNameCloseButton, Type = typeof(ButtonBase))]
	public class FloatingWindow : FloatingControl
	{
		#region Parts
		private const string PartNameCloseButton = "CloseButton";
		private ButtonBase PartCloseButton;
		#endregion

		#region DependencyProperties
		private static readonly DependencyProperty TitleProperty = DependencyProperty.Register("Title", typeof(string), typeof(FloatingWindow), new PropertyMetadata("Window Title"));
		public string Title
		{
			get { return (string)this.GetValue(TitleProperty); }
			set { this.SetValue(TitleProperty, value); }
		}
		#endregion

		#region Events
		public event EventHandler Closed;
		#endregion

		private UIElement rootVisual { get { return Application.Current.RootVisual; } }
		public new Panel Parent
		{
			get { return base.Parent as Panel; }
			set
			{
				if (this.Parent != null)
					this.Parent.Children.Remove(this);
				value.Children.Add(this);
			}
		}
		public bool IsShown { get { return base.Parent != null && this.Visibility == System.Windows.Visibility.Visible; } }

		public FloatingWindow()
			: base()
		{
			this.DefaultStyleKey = typeof(FloatingWindow);
		}

		#region Overriden Methods
		public override void OnApplyTemplate()
		{
			base.OnApplyTemplate();

			if (this.PartCloseButton != null)
				this.PartCloseButton.Click -= PartCloseButton_Click;

			this.PartCloseButton = this.GetTemplateChild(PartNameCloseButton) as ButtonBase;

			if (this.PartCloseButton != null)
				this.PartCloseButton.Click += PartCloseButton_Click;
		}

		protected override void OnPartMouseLeftButtonDown(FloatingControl.PartName Part, MouseButtonEventArgs e)
		{
			base.OnPartMouseLeftButtonDown(Part, e);
			this.rootVisual.MouseMove += this.OnUIParentMouseMove;
			//this.CaptureMouse();
		}

		protected override void OnRootVisualMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
		{
			base.OnRootVisualMouseLeftButtonUp(sender, e);
			this.rootVisual.MouseMove -= this.OnUIParentMouseMove;
			//this.ReleaseMouseCapture();
		}
		#endregion

		void PartCloseButton_Click(object sender, RoutedEventArgs e)
		{
			this.OnClosed();
		}

		private void TrySetParent()
		{
			FrameworkElement f = this.rootVisual as FrameworkElement;
			if (f != null)
				this.Parent = f.FindName("LayoutRoot") as Panel;
		}

		public bool Show()
		{
			if (this.Parent == null)
				this.TrySetParent();
			if (this.Parent == null)
				return false;

			this.Visibility = System.Windows.Visibility.Visible;
			return true;
		}

		public void Close()
		{
			this.OnClosed();
		}

		protected virtual void OnClosed()
		{
			this.Visibility = System.Windows.Visibility.Collapsed;
			if (this.Closed != null)
				this.Closed(this, EventArgs.Empty);
		}

		//public void BringToFront()
		//{
		//	if (this.IsShown == false)
		//		return;
		//	this.Parent.Children.Remove(this);
		//	this.Parent.Children.Add(this);
		//}
	}
}
